﻿/* 
   Copyright 2011 LUCA Studios LLC & John Evans

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

namespace LUCAStudios.Silverlight.Controls
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using DragDrop;

    internal class EdgeDockingDropZone : LUCADropZoneBase
    {
        private readonly InsertType _insertType;

        private readonly Grid _hotSpot = new Grid
                                             {
                                                 Background = new SolidColorBrush(Colors.Transparent)
                                             };

        private readonly Border _highlightingBorder = new Border
        {
            Background =
                new SolidColorBrush(Color.FromArgb(TargetVisualTransparancy, 200, 200, 200)),
            BorderThickness = new Thickness(0),
            BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0)),
            IsHitTestVisible = false,
            Visibility = Visibility.Collapsed
        };

        public EdgeDockingDropZone(InsertType insertType, IDragDropTarget source, Rect dimensions, Thickness dropZoneOffsets)
            : base(source, dimensions, dropZoneOffsets)
        {
            _insertType = insertType;
            InitUIProperties();
        }

        internal InsertType InsertType { get { return _insertType; } }

        private void InitUIProperties()
        {
            var g = DropZoneUI as Grid;
            if (g == null) return;

            g.Background = null;

            g.Children.Add(_highlightingBorder);
            g.Children.Add(_hotSpot);

            HotSpot = _hotSpot;

            switch (_insertType)
            {
                case InsertType.Top:
                    _hotSpot.Height = AdjustedTargetSize(TargetDimensions.Height * DropTargetRelativeSize);
                    _hotSpot.VerticalAlignment = VerticalAlignment.Top;
                    _hotSpot.HorizontalAlignment = HorizontalAlignment.Stretch;
                    _hotSpot.Margin = new Thickness(0, DropZoneOffsets.Top, 0, 0);

                    _highlightingBorder.Height = TargetDimensions.Height * TargetVisualRelativeSize;
                    _highlightingBorder.VerticalAlignment = VerticalAlignment.Top;
                    _highlightingBorder.HorizontalAlignment = HorizontalAlignment.Stretch;
                    _highlightingBorder.BorderThickness = new Thickness(0, DefaultBorderThickness, 0, 0);
                    break;
                case InsertType.Bottom:
                    _hotSpot.Height = AdjustedTargetSize(TargetDimensions.Height * DropTargetRelativeSize);
                    _hotSpot.VerticalAlignment = VerticalAlignment.Bottom;
                    _hotSpot.HorizontalAlignment = HorizontalAlignment.Stretch;
                    _hotSpot.Margin = new Thickness(0, 0, 0, DropZoneOffsets.Bottom);

                    _highlightingBorder.Height = TargetDimensions.Height * TargetVisualRelativeSize;
                    _highlightingBorder.VerticalAlignment = VerticalAlignment.Bottom;
                    _highlightingBorder.HorizontalAlignment = HorizontalAlignment.Stretch;
                    _highlightingBorder.BorderThickness = new Thickness(0, 0, 0, DefaultBorderThickness);
                    break;
                case InsertType.Left:
                    _hotSpot.Width = AdjustedTargetSize(TargetDimensions.Width * DropTargetRelativeSize);
                    _hotSpot.VerticalAlignment = VerticalAlignment.Stretch;
                    _hotSpot.HorizontalAlignment = HorizontalAlignment.Left;
                    _hotSpot.Margin = new Thickness(DropZoneOffsets.Left, 0, 0, 0);

                    _highlightingBorder.Width = TargetDimensions.Width * TargetVisualRelativeSize;
                    _highlightingBorder.VerticalAlignment = VerticalAlignment.Stretch;
                    _highlightingBorder.HorizontalAlignment = HorizontalAlignment.Left;
                    _highlightingBorder.BorderThickness = new Thickness(DefaultBorderThickness, 0, 0, 0);
                    break;
                case InsertType.Right:
                    _hotSpot.Width = AdjustedTargetSize(TargetDimensions.Width * DropTargetRelativeSize);
                    _hotSpot.VerticalAlignment = VerticalAlignment.Stretch;
                    _hotSpot.HorizontalAlignment = HorizontalAlignment.Right;
                    _hotSpot.Margin = new Thickness(0, 0, DropZoneOffsets.Right, 0);

                    _highlightingBorder.Width = TargetDimensions.Width * TargetVisualRelativeSize;
                    _highlightingBorder.VerticalAlignment = VerticalAlignment.Stretch;
                    _highlightingBorder.HorizontalAlignment = HorizontalAlignment.Right;
                    _highlightingBorder.BorderThickness = new Thickness(0, 0, DefaultBorderThickness, 0);
                    break;
            }
        }

        protected override void OnDropZoneEntered()
        {
            _highlightingBorder.Visibility = Visibility.Visible;
        }

        protected override void OnDropZoneLeave()
        {
            _highlightingBorder.Visibility = Visibility.Collapsed;
        }

        protected override void OnDropZoneDropped()
        {
            _highlightingBorder.Visibility = Visibility.Collapsed;
        }
    }
}
